package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.moving.R;
import com.lanou.moving.activity.WebViewActivity;
import com.lanou.moving.adapter.MusicThemeAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.MusicThemeRoot;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by 梁树珍 on 15/10/31.
 */

public class MusicThemeFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView music_theme_listView;
    private MusicThemeAdapter musicThemeAdapter;
    private List<MusicThemeRoot.DataEntity> entityList;
    private VolleySingleton volleySingleton;
    private GsonRequest<MusicThemeRoot> gsonRequest;
    private ImageView theme_back;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music_theme, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        music_theme_listView = (ListView) view.findViewById(R.id.music_theme_listView);
        music_theme_listView.setOnItemClickListener(this);
        theme_back = (ImageView) view.findViewById(R.id.theme_back);
        theme_back.setOnClickListener(this);
        getMusicTheme();
        return view;
    }

    private void getMusicTheme(){
        gsonRequest = new GsonRequest<>(URL.URL_MUSIC, MusicThemeRoot.class, new Response.Listener<MusicThemeRoot>() {
            @Override
            public void onResponse(MusicThemeRoot response) {
                entityList = response.getData();
                musicThemeAdapter = new MusicThemeAdapter(mContext, entityList);
                music_theme_listView.setAdapter(musicThemeAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(gsonRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MusicThemeRoot.DataEntity entity = (MusicThemeRoot.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("music_theme", entity);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
